package cn.wolfcode.consumer._03_consumer_mode;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author: 乱梦天真
 * @Description: TODO
 * @Date: 2020/5/10 17:41
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者
        DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("wolfcode_consumer");
        //2.绑定Namesrv
        pushConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3.订阅消费哪一个topic中的消息
        pushConsumer.subscribe("03-consumer-mode","*");
        //TODO 使用集群模式消费
        pushConsumer.setMessageModel(MessageModel.CLUSTERING);
        //4.监听消息列队中是否有消息传输过来,有就进行消费
        pushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println( "消费者1消息内容:"+new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.消费消息
        pushConsumer.start();
    }
}